using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text;

namespace SDK.Scripts.Utils
{
	public class Common
	{
		public delegate void SafeFunc();

		public delegate void ThreadFunc();

		private static DateTime _start = DateTime.Now;

		private static readonly object threadLock = new object();

		private static Queue<ThreadFunc> qThreadFunc = new Queue<ThreadFunc>();

		public static int Tick => (int)(DateTime.Now - _start).TotalMilliseconds;

		public static string GetFunctionName(string con = "")
		{
			StringBuilder name = new StringBuilder();
			SafeCall(delegate
			{
				if (0 == 0)
				{
					StackTrace stackTrace = new StackTrace();
					StackFrame frame = stackTrace.GetFrame(1);
					MethodBase method = frame.GetMethod();
					string name2 = method.ReflectedType.Name;
					if (name2.Equals("Common") && method.Name.Equals("SafeCall"))
					{
						name.Append("堆栈:").Append(stackTrace.ToString()).Append("------>>")
							.Append(con);
					}
					else
					{
						name.Append(name2).Append(".").Append(method.Name)
							.Append("------>>")
							.Append(con);
					}
				}
			});
			return name.ToString();
		}

		public static void SafeCall(SafeFunc f, SafeFunc fError = null)
		{
			try
			{
				f?.Invoke();
			}
			catch (Exception)
			{
				fError?.Invoke();
			}
		}

		public static void ChildThreadCall(ThreadFunc f)
		{
			lock (threadLock)
			{
				qThreadFunc.Enqueue(f);
			}
		}

		public static void SendMessageByMainThread()
		{
			lock (threadLock)
			{
				int num = 0;
				while (qThreadFunc.Count > 0 && num < 5)
				{
					num++;
					qThreadFunc.Dequeue()?.Invoke();
				}
			}
		}
	}
}
